<template>
  <el-dialog
    class="sortChartDialog"
    :model-value="centerDialogVisible"
    @close="closeDialog"
    title="便捷入口设置"
    width="500"
    align-center
  >
    <div class="content">
      <div class="left">
        <div class="header">可选功能</div>
        <div class="abilityList">
          <div class="modules" v-for="(item, index) in menuList" :key="index">
            <span class="title">{{ item.meta.title }}</span>
            <div class="module">
              <div
                class="moduleItem"
                v-for="(itemChild, indexChild) in item.children || []"
                :key="itemChild.id"
              >
                <div class="iconItem">
                  <div :style="{ minHeight: itemChild.icon ? 'auto' : '36px' }">
                    <i :class="['iconfont', itemChild.icon]"></i>
                  </div>
                  <span class="name">{{ itemChild.meta.title }}</span>
                </div>
                <el-checkbox
                  class="iconCheckbox"
                  :checked="itemChild.is_pick"
                  :modelValue="itemChild.is_pick"
                  size="large"
                  @change="toggleSelection(index, indexChild)"
                />
              </div>
            </div>
          </div>
        </div>
      </div>

      <div class="right">
        <div class="header">已选功能排序</div>
        <div class="chooseedList">
          <div class="modules" v-for="(item, index) in menuList" :key="index">
            <div v-if="item.children?.some((child) => child.is_pick)">
              <span class="title">{{ item.meta.title }}</span>
              <div class="module">
                <VueDraggable
                  ref="el"
                  v-model="item.children"
                  :animation="150"
                  ghostClass="ghost"
                  class="flex-col gap-2 p-4 w-300px h-300px m-auto bg-gray-500/5 rounded"
                >
                  <div
                    v-for="(itemChild, indexChild) in item.children || []"
                    :key="indexChild"
                  >
                    <div class="moduleItem" v-if="itemChild.is_pick">
                      <div class="iconItem">
                        <div class="iconDrag">
                          <i :class="['iconfont', 'icon-caidan']"></i>
                          <span class="name">{{ itemChild.meta.title }}</span>
                        </div>
                        <i
                          :class="['iconfont', 'icon-shanchu']"
                          @click="toggleSelection(index, indexChild)"
                        ></i>
                      </div>
                    </div>
                  </div>
                </VueDraggable>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <span class="defultButton" @click="resetMenuList">恢复默认设置</span>
        <div>
          <el-button @click="closeDialog">取消</el-button>
          <el-button type="primary" @click="primaryDialog">确认</el-button>
        </div>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { VueDraggable } from 'vue-draggable-plus';
import { nextTick, ref, watch } from 'vue';
import { MenuItem } from '@/types/dashboard/fastmenu/index';
import { extractIds } from './helper';
// 定义类型明确的响应式变量
const menuList = ref<MenuItem[]>([]);
// 在子组件中声明一个变量来存储默认状态
const defaultState = ref<MenuItem[]>([]);
// 弹窗状态和事件
const emit = defineEmits(['update:centerDialogVisible', 'updateMenuList']);

// 定义 Props
const props = defineProps({
  centerDialogVisible: {
    type: Boolean,
    required: true,
  },
  menuJson: {
    type: Array as () => MenuItem[], // 类型明确
    default: () => [],
  },
});

// 通用处理函数，支持传递选项更新
const handleDialogAction = async (menuListUpdate?: number[]) => {
  if (menuListUpdate) {
    emit('updateMenuList', menuListUpdate); // 仅在需要更新 menuList 时触发
  }
};

// 关闭弹窗，仅触发关闭逻辑
const closeDialog = () => {
  menuList.value = JSON.parse(JSON.stringify(defaultState.value)); // 恢复为默认状态
  emit('update:centerDialogVisible', false);
};

// 确认操作，同时关闭弹窗并更新勾选的菜单列表
const primaryDialog = () => {
  emit('update:centerDialogVisible', false);
  handleDialogAction(extractIds(menuList.value));
};

// 恢复默认设置，仅更新菜单列表，不关闭弹窗
const resetMenuList = () => {
  handleDialogAction([]);
};

// 切换选中状态
const toggleSelection = (index: number, indexChild: number) => {
  if (menuList.value.length) {
    nextTick(() => {
      const child = menuList.value[index]?.children?.[indexChild];
      if (child) {
        child.is_pick = !child.is_pick;
      }
    });
  }
};

// 监听 Props 变化
watch(
  () => props.menuJson,
  (newVal: MenuItem[]) => {
    menuList.value = JSON.parse(JSON.stringify(newVal)); // 创建副本
    defaultState.value = JSON.parse(JSON.stringify(newVal)); // 保存初始状态为 defaultState
  },
  { immediate: true } // 确保初始值也被监听
);
</script>

<style lang="scss" scoped src="./index.scss"></style>
